package com.wizinno.android.urineflow.util;

import android.os.Environment;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.wizinno.android.urineflow.constant.CommonControl;
import com.wizinno.android.urineflow.mqtt.vo.InspectDataVO;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author LiuMei
 * @date 2020-12-02.
 */
public class FileUtil {
    public static final String PATH_DATA = Environment.getExternalStorageDirectory().getPath() + "/urineflow/data";
    public static final String PATH_INSPECT_NO = PATH_DATA + "/inspect_no.txt";
    public static final String PATH_INSPECT_DATA = PATH_DATA + "/inspect_data.txt";
    public static final String PATH_DEVICE_COMPANY = PATH_DATA + "/device_company.txt";

    public static void createDir() {
        File path = new File(PATH_DATA);
        if (!path.exists()) {
            boolean mkdirs = path.mkdirs();
            if (mkdirs) {
                System.out.println("创建目录成功:" + path.getAbsolutePath());
            }
        }
    }

    public static String readFile(String fileName) {
        String data = null;
        File file = new File(fileName);
        if (file.exists()) {
            BufferedReader reader = null;
            StringBuffer sbf = new StringBuffer();
            try {
                reader = new BufferedReader(new FileReader(file));
                while ((data = reader.readLine()) != null) {
                    sbf.append(data);
                }
                reader.close();
                return sbf.toString();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (reader != null) {
                    try {
                        reader.close();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
            }
        }
        return data;
    }

    /**
     * 写出文件内容到文件
     */
    public static void writeFile(String fileName, String fileContent) {
        if (CommonControl.INSTANCE.isStoragePermission) {
            LogUtils.d("文件存储" + fileName + ":" + fileContent);
            BufferedWriter out = null;
            try {
                File file = new File(fileName);
                if (!file.exists()) {
                    boolean newFile = file.createNewFile();
                    if (!newFile) {
                        LogUtils.e("创建文件失败：" + fileName);
                    }
                }
                out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, false)));
                out.write(fileContent);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (out != null) {
                        out.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 拼接检查数据到文件
     *
     * @param data
     */
    public static void writeInspectData(InspectDataVO data) {
        List<InspectDataVO> dataList = readInspectData();
        dataList.add(data);
        writeInspectData(dataList);
    }

    /**
     * 覆盖原数据
     *
     * @param dataList
     */
    public static void writeInspectData(List<InspectDataVO> dataList) {
        writeFile(PATH_INSPECT_DATA, JSON.toJSONString(dataList));
    }

    public static List<InspectDataVO> readInspectData() {
        String dataJson = readFile(PATH_INSPECT_DATA);
        List<InspectDataVO> dataList = new ArrayList<>();
        if (StrUtil.isNotBlank(dataJson)) {
            dataList = JSON.parseArray(dataJson, InspectDataVO.class);
        }
        return dataList;
    }

    public static String readInspectNo() {
        String data = readFile(PATH_INSPECT_NO);
        return data;
    }

    public static void writeInspectNo(String data) {
        writeFile(PATH_INSPECT_NO, data);
    }

    public static String readDeviceCompany() {
        String data = readFile(PATH_DEVICE_COMPANY);
        return data;
    }

    public static void writeDeviceCompany(String data) {
        writeFile(PATH_DEVICE_COMPANY, data);
    }

}
